package com.example.administrator.test;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private  MyApplication app;
    private  MyApplication app2;

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private boolean isRemember;

    private Button login;
    private Button register;
    private EditText account;
    private EditText password;
    private CheckBox check;
    private TextView text_account;
    private TextView text_password;

    ProgressBar bar;
    TextView bar_size;

    private TextView forget;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }
        initView();
        getMessage();//历史记录是否记住密码 ，如果是则将账号与密码放在输入框
    }

    private void getMessage() {
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        isRemember = pref.getBoolean("flag", false);
        if(isRemember)
        {
            String x = pref.getString("account", "");
            String y = pref.getString("password", "");
            account.setText(x);
            password.setText(y);
            check.setChecked(true);
        }
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        login = (Button)findViewById(R.id.login_button);
        login.setOnClickListener(this);

        register = (Button)findViewById(R.id.register_button);
        register.setOnClickListener(this);

        account = (EditText)findViewById(R.id.login_edit_account);
        password = (EditText)findViewById(R.id.login_edit_password);
        check = (CheckBox)findViewById(R.id.login_checkbox);
        text_account = (TextView)findViewById(R.id.login_text_account);
        text_password = (TextView)findViewById(R.id.login_text_password);

        forget = (TextView)findViewById(R.id.login_text_forget);
        forget.setOnClickListener(this);

        bar = (ProgressBar)findViewById(R.id.login_progress);
        bar_size = (TextView)findViewById(R.id.login_progress_size);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_button:
                bar.setVisibility(View.VISIBLE);
                bar_size.setVisibility(View.VISIBLE);
                bar.setMax(100);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=1;i<=100;i++){
                            Message message = new Message();
                            message.what = 1;
                            message.getData().putInt("size",i);
                            handler.sendMessage(message);
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();

//                if(account.getText().toString().equals("")||password.getText().toString().equals("")){
//                    Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                app = new MyApplication();
//                DbManager db = x.getDb(app.getDaoConfig());
//                try {
//                    User user = db.selector(User.class).where("name","==",account.getText().toString()).findFirst();
//                    if(user==null){
//                        Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
//                        return;
//                    }else{
//                        if(user.getPassword().equals(password.getText().toString())){
//                            editor = pref.edit();
//                            if (check.isChecked())
//                            {
//                                editor.putBoolean("flag",true);
//                                editor.putString("account", account.getText().toString());
//                                editor.putString("password",password.getText().toString());
//                            }
//                            else
//                            {
//                                editor.clear();
//                            }
//                            editor.commit();
//                            app2 = (MyApplication)getApplication();
//                            app2.setAccount(account.getText().toString());
//                            Intent login_intent = new Intent(LoginActivity.this,MainActivity.class);
//                            startActivity(login_intent);
//                            finish();
//                        }else{
//                            Toast.makeText(LoginActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                } catch (DbException e) {
//                    e.printStackTrace();
//                }


                break;
            case R.id.register_button:
                Intent register = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(register);
                break;

            case R.id.login_text_forget:
                Intent forget = new Intent(LoginActivity.this,ForgetPasswordActivity.class);
                startActivity(forget);
                break;
        }
    }

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case 1:
                    int size = msg.getData().getInt("size");
                    bar.setProgress(size);
                    bar_size.setText(size+"%");
                    if(size==100){
                        bar.setProgress(0);
                        bar_size.setVisibility(View.GONE);
                        bar.setVisibility(View.GONE);
                        if (account.getText().toString().equals("") || password.getText().toString().equals("")) {
                            Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        app = new MyApplication();
                        DbManager db = x.getDb(app.getDaoConfig());
                        try {
                            User user = db.selector(User.class).where("name", "==", account.getText().toString()).findFirst();
                            if (user == null) {
                                Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                                return;
                            } else {
                                if (user.getPassword().equals(password.getText().toString())) {
                                    editor = pref.edit();
                                    if (check.isChecked()) {
                                        editor.putBoolean("flag", true);
                                        editor.putString("account", account.getText().toString());
                                        editor.putString("password", password.getText().toString());
                                    } else {
                                        editor.clear();
                                    }
                                    editor.commit();
                                    app2 = (MyApplication) getApplication();
                                    app2.setAccount(account.getText().toString());
                                    Intent login_intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(login_intent);
                                    finish();
                                } else {
                                    Toast.makeText(LoginActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
        }
    };

}
